App({
  onLaunch: function () {
    const info = wx.getSystemInfoSync();
    this.globalData.sysinfo = info;
    this.globalData.screenWidth = info.screenWidth;
    this.globalData.screenHeight = info.screenHeight;
    try {
      // 初始化云开发环境
      //wx6cdbd992e615c35e
      wx.cloud.init({
        env: "xiegang-3gf3n6ez27c3c8e1",
      });

      // 声明新的 cloud 实例
      this.crossCloud = new wx.cloud.Cloud({
        resourceAppid: "wxa26852173b169ce2",
        resourceEnv: "xiegang-3gf3n6ez27c3c8e1",
      });

      // 跨账号调用，必须等待 init 完成
      this.crossCloud
        .init()
        .then(() => {
          console.log("跨账号云开发实例初始化成功");

          // 初始化数据库对象
          this.globalData.db = this.crossCloud.database();
        })
        .catch((err) => {
          console.error("跨账号云开发实例初始化失败: ", err);
        });
    } catch (err) {
      console.error("初始化失败: ", err);
    }
    //自动更新操作
    const updateManager = wx.getUpdateManager();

    updateManager.onCheckForUpdate(function (res) {
      // 请求完新版本信息的回调
      if (res.hasUpdate) {
        updateManager.onUpdateReady(function () {
          wx.showModal({
            title: "更新提示",
            content: "新版本已经准备好，是否重启应用？",
            success: function (res) {
              if (res.confirm) {
                // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                updateManager.applyUpdate();
              }
            },
          });
        });

        updateManager.onUpdateFailed(function () {
          // 新的版本下载失败
          wx.showModal({
            title: "更新提示",
            content: "新版本下载失败，请稍后再试",
            showCancel: true,
          });
        });
      }
    });
  },

  // 获取数据库对象
  getDatabase: function () {
    return this.globalData.db;
  },

  getModel: function () {
    //获取手机型号
    return this.globalData.sysinfo["model"];
  },
  getVersion: function () {
    //获取微信版本号
    return this.globalData.sysinfo["version"];
  },
  getSystem: function () {
    //获取操作系统版本
    return this.globalData.sysinfo["system"];
  },
  getPlatform: function () {
    //获取客户端平台
    return this.globalData.sysinfo["platform"];
  },
  getSDKVersion: function () {
    //获取客户端基础库版本
    return this.globalData.sysinfo["SDKVersion"];
  },
  globalData: {
    userInfo: null,
    platform: "",

    db: null,
    screenWidth: 0,
    screenHeight: 0,
  },
  BLEInformation: {
    platform: "",
    deviceId: "",
    writeCharaterId: "",
    writeServiceId: "",
    notifyCharaterId: "",
    notifyServiceId: "",
    readCharaterId: "",
    readServiceId: "",
  },
});
